include ../Makefile.common

LIBRARY = lib$(NAME)
MAJOR = 1

SHAREDLIB = $(LIBRARY).so
SONAME = $(SHAREDLIB).$(MAJOR)
STATICLIB = $(LIBRARY).a

INSTALL = install

DESTDIR = $(RPM_BUILD_ROOT)
LIBDIR = $(DESTDIR)/usr/lib
INCLUDEDIR = $(DESTDIR)/usr/include/ncurses

CXXFLAGS =$(FLAGS) -I../include $(RPM_OPT_FLAGS)
LINK.o = $(CXX) $(LDFLAGS) $(FLAGS) $(TARGET_ARCH)

LIB_SRC = terminal.cc assert.cc window.cc application.cc color.cc pad.cc panel.cc

LIB_OBJ = $(LIB_SRC:%.cc=%.o)
LIB_SOBJ = $(LIB_SRC:%.cc=%.so)
LIB_DEP = $(LIB_SRC:%.cc=%.d)

TARGETS = $(SHAREDLIB) $(STATICLIB)

.PHONY: all install clean

all: $(TARGETS)

%.so: %.cc
	$(COMPILE.cpp) $(CPPFLAGS) -fpic $< $(OUTPUT_OPTION)

$(SHAREDLIB): $(LIB_SOBJ)
	$(LINK.o) -shared -Wl,-soname,$(SONAME) -lc $+ $(OUTPUT_OPTION)
	ln -sf $(SHAREDLIB) $(SONAME)

$(STATICLIB): $(LIB_OBJ)
	$(AR) $(ARFLAGS) $@ $+
	-ranlib $@

install: all
	$(INSTALL) -d -m755 $(INCLUDEDIR)
	@for i in ../include/ncurses/*.hh; do $(INSTALL) $$i -pm644 $(INCLUDEDIR) ;done
	$(INSTALL) -pD -m755 $(SHAREDLIB) $(LIBDIR)/$(SHAREDLIB).$(VERSION)
	$(INSTALL) -pD -m644 $(STATICLIB) $(LIBDIR)/$(STATICLIB)
	@ln -sf $(SHAREDLIB).$(VERSION) $(LIBDIR)/$(SONAME)
	@ln -sf $(SONAME) $(LIBDIR)/$(SHAREDLIB)

clean:
	$(RM) $(TARGETS) $(LIB_OBJ) $(LIB_SOBJ) $(LIB_DEP) $(SONAME) core *~ test

# We need dependencies only if goal isn't "clean"
ifneq ($(MAKECMDGOALS),clean)

%.d:	%.cc
	@echo Making dependences for $<
	@$(SHELL) -ec "$(CC) -MM $(CXXFLAGS) $< | sed 's|\($*\)\.o[ :]*|\1.o $@ : |g' > $@; [ -s $@ ] || $(RM) $@"

ifneq ($(LIB_DEP),)
-include $(LIB_DEP)
endif
endif
